package kinect.viewer;

import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.HashMap;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import kinect.storage.KinectStorageProtos.Frame;
import kinect.storage.KinectStorageProtos.Skeleton;
import kinect.storage.KinectStorageProtos.Joint;
import kinect.storage.KinectStorageProtos.Point3D;
import kinect.storage.KinectStorageProtos.Joint.JointType;
import kinect.util.PrintfFormat;

/**
 *
 * @author samo
 */
public class SkeletonEdit extends javax.swing.JFrame {
    private Frame frame;
    private Skeleton skeleton;
    private HashMap<JointType, String> jointNameMap;

    
    /** Creates new form SkeletonEdit */
    public SkeletonEdit() {
        initComponents();
    }

    public SkeletonEdit(Frame frame) {
        initComponents();
        
        this.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        this.frame = frame;
        this.jointNameMap = new HashMap<>();
        initJointNameMap();
        initEdit();
    }

    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelSkeleton = new javax.swing.JPanel();
        jPanelInfo = new javax.swing.JPanel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Skeleton");
        setName("frameEdit"); // NOI18N

        jPanelSkeleton.setLayout(new java.awt.GridBagLayout());

        jPanelInfo.setLayout(new java.awt.GridBagLayout());

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanelSkeleton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanelInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(592, 592, 592))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanelSkeleton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelInfo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(525, 525, 525))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    
    /*
     * 
     */
    private void initJointNameMap() {
        jointNameMap.put(JointType.HEAD, "Head");
        jointNameMap.put(JointType.NECK, "Neck");
        jointNameMap.put(JointType.TORSO, "Torso");

        jointNameMap.put(JointType.LEFT_SHOULDER, "Left Shoulder");
        jointNameMap.put(JointType.LEFT_ELBOW, "Left Elbow");
        jointNameMap.put(JointType.LEFT_HAND, "Left Hand");

        jointNameMap.put(JointType.RIGHT_SHOULDER, "Right Shoulder");
        jointNameMap.put(JointType.RIGHT_ELBOW, "Right Elbow");
        jointNameMap.put(JointType.RIGHT_HAND, "Right Hand");

        jointNameMap.put(JointType.LEFT_HIP, "Left Hip");
        jointNameMap.put(JointType.LEFT_KNEE, "Left Knee");
        jointNameMap.put(JointType.LEFT_FOOT, "Left Foot");

        jointNameMap.put(JointType.RIGHT_HIP, "Right Hip");
        jointNameMap.put(JointType.RIGHT_KNEE, "Right Knee");
        jointNameMap.put(JointType.RIGHT_FOOT, "Right Foot");
    }
    
    private void initEdit() {
        this.skeleton = frame.getSkeleton();     
        GridBagConstraints c = new GridBagConstraints();
        PrintfFormat pf1 = new PrintfFormat("%3.0f");
        PrintfFormat pf2 = new PrintfFormat("%4.0f");
        NumberFormat nf1 = new DecimalFormat();
        nf1.setMaximumIntegerDigits(3);
        nf1.setMinimumIntegerDigits(3);
        nf1.setMaximumFractionDigits(1);
        nf1.setMinimumFractionDigits(1);
        NumberFormat nf2 = new DecimalFormat();
        nf2.setGroupingUsed(false);
        nf2.setMaximumIntegerDigits(4);
        nf2.setMinimumIntegerDigits(4);
        nf2.setMaximumFractionDigits(1);
        nf2.setMinimumFractionDigits(1);
        NumberFormat nf3 = new DecimalFormat();
        nf3.setGroupingUsed(false);
        nf3.setMaximumIntegerDigits(1);
        nf3.setMinimumIntegerDigits(1);
        nf3.setMaximumFractionDigits(1);
        nf3.setMinimumFractionDigits(1);
        JPanel panel;
        Point3D point;
        JLabel label;
        JFormattedTextField textField;

        c.fill = GridBagConstraints.CENTER;
        c.gridy = 0;
        c.gridx = 0;
        jPanelSkeleton.add(new JLabel(""),c);
        c.gridx = 1;
        jPanelSkeleton.add(new JLabel("Projective"),c);
        c.gridx = 2;
        jPanelSkeleton.add(new JLabel("Real World"),c);
        c.gridx = 3;
        jPanelSkeleton.add(new JLabel("Confidence"),c);

        c.ipady = 5;
        for(Joint joint:skeleton.getJointsList()) {
            c.fill = GridBagConstraints.HORIZONTAL;
            c.gridy++;
            /*
             * Joint Name
             */
            c.gridx = 0;
            jPanelSkeleton.add(new JLabel(jointNameMap.get(joint.getType())), c);
            
            /*
             * Projective 
             */
            c.gridx = 1;
            c.fill = GridBagConstraints.CENTER;
            //panel.add(new JLabel(jointNameMap.get(joint.getType())));
            point = joint.getProjective();
            
//            panel.add(new JLabel(" [x: "+pf1.sprintf(point.getX())));
//            panel.add(new JLabel(", y: "+pf1.sprintf(point.getY())));
//            panel.add(new JLabel(", z: "+pf1.sprintf(point.getZ())+"]"));
            jPanelSkeleton.add(new JLabel("   [x: "+pf1.sprintf(point.getX())+
                                ", y: "+pf1.sprintf(point.getY())+
                                ", z: "+pf2.sprintf(point.getZ())+"]  "),c);
            
            /*
             * Real World 
             */
//            c.gridx = 2;
//            panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
//            point = joint.getRealWorld();
//            label = new JLabel(" x: "); 
//            panel.add(label);
//            textField = new JFormattedTextField(nf1);
//            textField.setValue(point.getX());
//            panel.add(textField);
//            label = new JLabel(" y: "); 
//            panel.add(label);
//            textField = new JFormattedTextField(nf1);
//            textField.setValue(point.getY());
//            panel.add(textField);
//            label = new JLabel(" z: "); 
//            panel.add(label);
//            textField = new JFormattedTextField(nf2);
//            textField.setValue(point.getZ());
//            panel.add(textField);
//            jPanelSkeleton.add(panel,c);
            
            c.gridx = 2;
            c.fill = GridBagConstraints.CENTER;
            point = joint.getRealWorld();
            jPanelSkeleton.add(new JLabel("   [x: "+pf1.sprintf(point.getX())+
                                ", y: "+pf1.sprintf(point.getY())+
                                ", z: "+pf2.sprintf(point.getZ())+"]  "),c);
            
            /*
             * Confidence
             */
//            c.fill = GridBagConstraints.CENTER;
//            c.gridx = 3;
//            textField = new JFormattedTextField(nf3);
//            textField.setValue(joint.getConfidence());
//            jPanelSkeleton.add(textField,c);
            c.fill = GridBagConstraints.CENTER;
            c.gridx = 3;
            jPanelSkeleton.add(new JLabel("   [x: "+pf1.sprintf(joint.getConfidence())+"]  "),c);
        }
        
        this.validate();
        
    }
    
//    private void createHash(Skeleton skeleton) {
//        jointHash = new HashMap<>();
//        jointHash.clear();
//        for(Joint joint:skeleton.getJointsList()) {
//            jointHash.put(joint.getType(), joint);
//        }
//    }
    
    
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); 
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SkeletonEdit.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SkeletonEdit().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel jPanelInfo;
    private javax.swing.JPanel jPanelSkeleton;
    // End of variables declaration//GEN-END:variables
}
